package com.gome.axwl.aoc.facade.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.entity.dto.DeviceDto;
import com.gome.axwl.aoc.entity.dto.DeviceMsgDto;
import com.gome.axwl.aoc.entity.dto.PageParamDto;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.entity.po.DeviceMsgPo;
import com.gome.axwl.aoc.entity.po.DevicePo;
import com.gome.axwl.aoc.facade.DeviceFacade;
import com.gome.axwl.aoc.service.DeviceService;
import com.gome.common.utils.StringUtil;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 登录设备管理
 * @Auther: chengh
 * @Date: 2018/4/25 13:59
 */

@Service
public class DeviceFacedeImpl implements DeviceFacade {


    private final static Logger log = LoggerFactory.getLogger(DeviceFacedeImpl.class);

    @Autowired
    private DeviceService deviceService;

    /**
     * 新增或者更新
     *
     * @param deviceDto
     * @return
     */
    @Override
    @Transactional
    public ResultData addDeviceInfo(DeviceDto deviceDto) {
        try {
            DevicePo devicePo = new DevicePo();
            devicePo.setCid(deviceDto.getCid());
            DevicePo devicePo1 = deviceService.selectSelective(devicePo);     //根据cid去查是否当前设备已有设备信息
            DevicePo devicePo2 = BeanJsonUtil.beanConvert(deviceDto, DevicePo.class);
            if (devicePo1 == null || StringUtil.isEmpty(devicePo1.getCid())) {   //cid没有
                deviceService.deleteByEmpNo(deviceDto.getEmpNo());      //删除当前账号以前的记录
                devicePo2.setCreateTime(new Date());
                deviceService.insert(devicePo2);
            } else {
                if(!devicePo1.getEmpNo().equals(deviceDto.getEmpNo())) {   //当前设备的登录账号不是现在登录的账号
                    deviceService.deleteByEmpNo(deviceDto.getEmpNo());      //删除当前账号以前的记录
                }
                    devicePo2.setModifyTime(new Date());
                    deviceService.updateByCid(devicePo2);      //将登录设备的账号信息修改为当前登录账号
            }
        } catch (Exception e) {
            log.error("更新用户登录设备信息失败，设备信息：{}，异常信息：{}", deviceDto.toString(), e.toString());
            return new ResultData(ErrorCode.FAILOR, ErrorCode.FAILOR_MSG);
        }
        return new ResultData(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG);
    }

    /**
     * 根据driverIds查询用户的登录设备信息集合
     * @param driverIds
     * @return
     */
    @Override
    public ResultData getDeviceInfoByDriverIds(List<String> driverIds) {
        try {
            List<DeviceMsgPo> deviceMsgPoList = deviceService.getDeviceInfoByDriverIds(driverIds);
            return new ResultData<>(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG, deviceMsgPoList);
        }catch (Exception e){
            log.error("查询设备信息失败，查询条件：{}，异常信息：{}", driverIds.toString(), e.toString());
            return new ResultData(ErrorCode.QUERY_FAILOR, ErrorCode.QUERY_FAILOR_MSG);
        }
    }

    /**
     * 分页查询设备信息
     * @param pageParamDto
     * @return
     */
    @Override
    public ResultData getDeviceInfoByPage(PageParamDto pageParamDto) {
        try {
            PageHelper.startPage(pageParamDto.getPageNum(), pageParamDto.getPageSize());   //设置分页参数
            List<DeviceMsgPo> deviceMsgPoList = deviceService.getDeviceInfoByPage();                //查询
            PageInfo<DeviceMsgPo> pageInfo = new PageInfo<>(deviceMsgPoList);
            PageResultDto<DeviceMsgDto> pageResultDto = new PageResultDto<>();
            List<DeviceMsgDto> deviceMsgDtoList = BeanJsonUtil.listConvert(deviceMsgPoList, DeviceMsgDto.class);
            pageResultDto.setData(deviceMsgDtoList);
            pageResultDto.setPageCount(pageInfo.getPages());
            pageResultDto.setPageNum(pageInfo.getPageNum());
            pageResultDto.setTotal(pageInfo.getTotal());
            pageResultDto.setPageSize(pageInfo.getPageSize());
            return new ResultData<>(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG, pageResultDto);
        } catch (Exception e) {
            log.error("分页查询设备信息失败，查询条件：{}，异常信息：{}", pageParamDto.toString(), e.toString());
            return new ResultData(ErrorCode.QUERY_FAILOR, ErrorCode.QUERY_FAILOR_MSG);
        }
    }

}
